// 订单模块，有自己的state、reducer、actions、actionType
const initState = {
  list: [
    {id:'1', name: 'macBook', price: 14999},
    {id:'2',name: 'mateBook', price: 9999},
    {id:'3',name: 'magicBook', price: 8999},
    {id:'4',name: 'miBook', price: 7999}
  ]
}

// getList这个函数的作用就是返回数据
export const getList = (state) => state.order.list

const actionType = {
  DELORDER: 'DELORDER',
  ADDORDER: 'ADDORDER'
}

export const actions = {
  delOrder: (index)=>({type: actionType.DELORDER, index}),
  addOrder: (info)=>({type: actionType.ADDORDER, info})
}

const orderReducer = (state=initState, action)=>{
  switch (action.type) {
    case actionType.DELORDER:
      let arr = state.list;
      arr.splice(action.index, 1)
      return {
        ...state,
        list: [...arr]
      }
    case actionType.ADDORDER:
      console.log(action);
      let arr1 = state.list;
      arr1.push(action.info);
      return {
        ...state,
        list: [...arr1]
      }
  
    default:
      return state
  }
}
export default orderReducer